﻿param($sourceId,$managedEntityId,$vCenterServerName)

Function ExitPrematurely ($Message) {
	$discoveryData.IsSnapshot = $false
	$api.LogScriptEvent($ScriptName,1985,2,$Message)
	$discoveryData
	exit
}

$ScriptName = 'Community.VMware.Discovery.HostDatastoreReference.ps1'
$api = new-object -comObject 'MOM.ScriptAPI'
$discoveryData = $api.CreateDiscoveryData(0, $sourceId, $managedEntityId)

Import-Module OperationsManager
New-SCOMManagementGroupConnection 'localhost'

$MGconn = Get-SCOMManagementGroupConnection | Where {$_.IsActive -eq $true}

If(!$MGconn){
	ExitPrematurely ("Unable to connect to the local management group")
}

#Get Already Discovered VM Hosts from SCOM
$VMhostObjs = Get-SCOMClass -Name 'Community.VMware.Class.Host' | Get-SCOMClassInstance | Where {$_.'[Community.VMware.Class.vCenter].vCenterServerName'.Value -eq $vCenterServerName}

#Exit if no VMs are discovered, because there is no relationship to build
If (!$VMhostObjs){
	ExitPrematurely ("No VM Hosts found discovered in SCOM for vCenter " + $vCenterServerName)
}

#Get Already Discovered Datastores from SCOM
$VMdatastoreObjs = Get-SCOMClass -Name 'Community.VMware.Class.Datastore' | Get-SCOMClassInstance | Where {$_.'[Community.VMware.Class.vCenter].vCenterServerName'.Value -eq $vCenterServerName}

If (!$VMdatastoreObjs){
	ExitPrematurely ("No VM Datastores found discovered in SCOM for vCenter " + $vCenterServerName)
}

Add-PSSnapin VMware.VimAutomation.Core
$connection = Connect-VIServer -Server $vCenterServerName -Force:$true

If ($connection.IsConnected -ne $True){
	ExitPrematurely ("Unable to connect to vCenter server " + $vCenterServerName)
}

$VMwareHosts = (Get-View -ViewType HostSystem -Property Datastore) | Select Datastore,MoRef

If (!$VMwareHosts){
	Disconnect-VIServer -Server $connection -Confirm:$false
	ExitPrematurely ("No VMs found in vCenter " + $vCenterServerName)
}

ForEach ($VMhost in $VMwareHosts){

	If ($VMhostObjs | Where {$_.'[Community.VMware.Class.Host].HostId'.Value -eq [string]$VMhost.MoRef}){

		#VM Host Obj (already discovered)
		$VMhostInstance = $discoveryData.CreateClassInstance("$MPElement[Name='Community.VMware.Class.Host']$")
		$VMhostInstance.AddProperty("$MPElement[Name='Community.VMware.Class.Host']/HostId$", [string]$VMhost.MoRef )
		$VMhostInstance.AddProperty("$MPElement[Name='Community.VMware.Class.vCenter']/vCenterServerName$", $vCenterServerName )
		
		ForEach ($VMdatastore in $VMhost.Datastore){

			$MatchingDatastore = $VMdatastoreObjs | Where {$_.'[Community.VMware.Class.Datastore].DatastoreId'.Value -eq [string]$VMdatastore}
			If ($MatchingDatastore){

				#Datastore Obj (already discovered)
				$VMdatastoreInstance = $discoveryData.CreateClassInstance("$MPElement[Name='Community.VMware.Class.Datastore']$")
				$VMdatastoreInstance.AddProperty("$MPElement[Name='Community.VMware.Class.Datastore']/DatastoreId$", [string]$MatchingDatastore.'[Community.VMware.Class.Datastore].DatastoreId'.Value )
				$VMdatastoreInstance.AddProperty("$MPElement[Name='Community.VMware.Class.vCenter']/vCenterServerName$", $vCenterServerName )
				
				#VM references Datastore
				$rel1 = $discoveryData.CreateRelationshipInstance("$MPElement[Name='Community.VMware.Relationship.HostReferencesDatastore']$")
				$rel1.Source = $VMhostInstance
				$rel1.Target = $VMdatastoreInstance
				$discoveryData.AddInstance($rel1)
			}
		}
	}
}
Disconnect-VIServer -Server $connection -Confirm:$false
$discoveryData